﻿<common:LayoutAwarePage
    x:Name="pageRoot"
    x:Class="EagleGO.KiFUPage"
    DataContext="{Binding DefaultViewModel, RelativeSource={RelativeSource Self}}"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation"
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml"
    xmlns:local="using:EagleGO"
    xmlns:common="using:EagleGO.Common"
    xmlns:d="http://schemas.microsoft.com/expression/blend/2008"
    xmlns:mc="http://schemas.openxmlformats.org/markup-compatibility/2006"
    xmlns:Controls="using:Bigeagle.Metro.Games.Controls"
    mc:Ignorable="d">

    <Page.Resources>
        <CollectionViewSource
            x:Name="gameViewSource"
            Source="{Binding Groups}"
            IsSourceGrouped="true"
            ItemsPath="MainGameTreeMoves"/>

        <DataTemplate x:Key="gameHeadViewSource">
            <Border BorderThickness="1">
            <Grid >
                <Grid.ColumnDefinitions>
                    <ColumnDefinition Width="60"/>
                    <ColumnDefinition Width="*"/>
                </Grid.ColumnDefinitions>
                <Button Tag="{Binding}" BorderThickness="0"  Grid.Column="0"  Content="&#xE0ba;" FontFamily="Segoe UI Symbol" Foreground="White" FontSize="18" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="BackButton_Tapped"/>
                <TextBlock Grid.Column="1" FontSize="18"  Text="{Binding GameInfo.Name}" HorizontalAlignment="Stretch" VerticalAlignment="Center" LineHeight="20"/>
            </Grid>
            </Border>
        </DataTemplate>

        <DataTemplate x:Key="movesItemSource">
                <Grid >
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="60"/>
                        <ColumnDefinition Width="*"/>
                    </Grid.ColumnDefinitions>
                <Button Tag="{Binding}" BorderThickness="0" Visibility="{Binding ReferenceGame,Converter={StaticResource visibilityConverter}}" Grid.Column="0"  Content="&#xE109;" FontFamily="Segoe UI Symbol" Foreground="White" FontSize="16" HorizontalAlignment="Center" VerticalAlignment="Center" Tapped="CollapseButton_Tapped" />
                    <TextBlock Grid.Column="1" Text="{Binding Converter={StaticResource moveStringConverter}}" HorizontalAlignment="Stretch" VerticalAlignment="Center" DoubleTapped="TextBlock_DoubleTapped" LineHeight="18" />
                </Grid>
        </DataTemplate>
    </Page.Resources>
    <Page.BottomAppBar>
        <AppBar>
            <AppBar.Background>
                <ImageBrush ImageSource="Assets/barback.jpg"/>
            </AppBar.Background>
        </AppBar>
    </Page.BottomAppBar>
    <Page.TopAppBar>
        <AppBar>
            <AppBar.Background>
                <ImageBrush ImageSource="Assets/barback.jpg"/>
            </AppBar.Background>
            <StackPanel Orientation="Horizontal">
                <Button x:Name="btnPlay" Content="双人对弈"  IsEnabled="True" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="Black" BorderThickness="1" Tapped="btnPlay_Tapped"/>
                <Button x:Name="btnKiFU" Content="打谱" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="Black" Tapped="btnKiFU_Tapped"/>
                <Button x:Name="btnPlayOnlie" Content="网上对弈" IsEnabled="False" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" Foreground="Black"/>
            </StackPanel>
        </AppBar>
    </Page.TopAppBar>

    <!--
        This grid acts as a root panel for the page that defines two rows:
        * Row 0 contains the back button and page title
        * Row 1 contains the rest of the page layout
    -->

    <Grid Style="{StaticResource LayoutRootStyle}">
        <Grid.Background>
            <ImageBrush ImageSource="Assets/background.jpg"/>
        </Grid.Background>
        <Grid.RowDefinitions>
            <RowDefinition Height="140"/>
            <RowDefinition Height="*"/>
        </Grid.RowDefinitions>

        <!-- Back button and page title -->
        <Grid>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="Auto"/>
                <ColumnDefinition Width="132*"/>
                <ColumnDefinition Width="491*"/>
            </Grid.ColumnDefinitions>
            <Button x:Name="backButton" Click="GoBack" IsEnabled="{Binding Frame.CanGoBack, ElementName=pageRoot}" Style="{StaticResource BackButtonStyle}" Margin="36,0,0,36"/>
            <TextBlock x:Name="pageTitle" Grid.Column="1" Text="{StaticResource AppName}" Style="{StaticResource PageHeaderTextStyle}"/>
            <Grid Grid.Column="2" Margin="0">
                <Border BorderBrush="Black" BorderThickness="1" Margin="10" Padding="10" CornerRadius="10" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}">
                    <Grid>
                        <Grid.ColumnDefinitions>
                            <ColumnDefinition Width="50*"></ColumnDefinition>
                            <ColumnDefinition Width="50*"></ColumnDefinition>
                        </Grid.ColumnDefinitions>
                        <Grid.RowDefinitions>
                            <RowDefinition Height="40*"></RowDefinition>
                            <RowDefinition Height="30*"></RowDefinition>
                            <RowDefinition Height="30*"></RowDefinition>
                        </Grid.RowDefinitions>
                        <TextBlock Text="{Binding GameInfo.Event}"  Style="{StaticResource BaselineTextStyle}" Grid.Column="0" FontSize="18" ></TextBlock>
                        <TextBlock Text="{Binding GameInfo.Name}"  Style="{StaticResource BaselineTextStyle}" Grid.Column="1" Grid.ColumnSpan="1" FontSize="22" FontWeight="Bold"></TextBlock>
                        <TextBlock Text="{Binding GameInfo.BlackPlayer}"  Style="{StaticResource BaselineTextStyle}" Grid.Column="0" Grid.Row="1" FontSize="16" ></TextBlock>
                        <TextBlock Text="{Binding GameInfo.WhitePlayer}"  Style="{StaticResource BaselineTextStyle}" Grid.Column="1" Grid.Row="1" FontSize="16" ></TextBlock>
                    </Grid>
                </Border>
            </Grid>

        </Grid>
        <Grid Grid.Row="1" Grid.Column="0"  Margin="10,10,10,10">
            <Grid.RowDefinitions>
                <RowDefinition Height="10"/>
                <RowDefinition Height="50"/>
                <RowDefinition Height="*"/>
            </Grid.RowDefinitions>
            <Grid.ColumnDefinitions>
                <ColumnDefinition Width="200*"/>
                <ColumnDefinition Width="500*"/>
                <ColumnDefinition Width="300*"/>
            </Grid.ColumnDefinitions>
            <Controls:BoardControl x:Name="_Board" Margin="0,0,0,0" BoardPositionClicked="BoardControl_BoardPositionClicked" StoneRadius="13" Grid.RowSpan="2" Grid.Row="1" Width="550" Height="550" Grid.Column="1" VerticalAlignment="Top" HorizontalAlignment="Center"  >
                <Controls:BoardControl.BoardBackground>
                    <ImageBrush Stretch="UniformToFill" ImageSource="/Assets/wood.jpg"/>
                </Controls:BoardControl.BoardBackground>
            </Controls:BoardControl>
            
            <Border BorderBrush="Black" BorderThickness="1" Grid.Column="2" Margin="10" Grid.Row="2"  Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}" CornerRadius="16" Padding="10">
                
                    <ListView x:Name="lvMoves" ItemsSource="{Binding Source={StaticResource gameViewSource}}" 
                          SelectionChanged="lvMoves_SelectionChanged" ItemTemplate="{StaticResource movesItemSource}">
                    <ListView.ItemsPanel>
                        <ItemsPanelTemplate>
                            <VirtualizingStackPanel Orientation="Vertical" >
                            </VirtualizingStackPanel>
                        </ItemsPanelTemplate>
                    </ListView.ItemsPanel>
                    <ListView.GroupStyle>
                        <GroupStyle HeaderTemplate="{StaticResource gameHeadViewSource}" ></GroupStyle>
                    </ListView.GroupStyle>

                </ListView>
            </Border>
            
            <Border BorderBrush="Black" BorderThickness="1" Grid.Column="0" Grid.Row="1" Grid.RowSpan="2" Margin="10" CornerRadius="16" Background="{StaticResource ListViewItemOverlayBackgroundThemeBrush}" Padding="16">
                <ScrollViewer>
                    <TextBlock x:Name="tbMoveComment" TextWrapping="Wrap"  Text="{Binding ElementName=lvMoves, Path=SelectedValue,Source=SelectedValue.Comment}" Style="{StaticResource BaselineTextStyle}" Margin="5"/>
                </ScrollViewer>
            </Border>
            <StackPanel Grid.Column="2" Grid.Row="0" Grid.RowSpan="2" Margin="10,5,5,5" Orientation="Horizontal" >
                <Button x:Name="btnFirst" Content="&lt;&lt;" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontFamily="Segoe UI Symbol" FontSize="24" Tapped="btnAutoPlay_Tapped"/>
                <Button x:Name="btnPrev" Content="&#xE100;" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontFamily="Segoe UI Symbol" FontSize="24" Tapped="btnAutoPlay_Tapped"/>
                <Button x:Name="btnAutoPlay" Content="&#xE102;" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontFamily="Segoe UI Symbol" FontSize="24" Tapped="btnAutoPlay_Tapped"/>
                <Button x:Name="btnNext" Content="&#xE101;" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontFamily="Segoe UI Symbol" FontSize="24" Tapped="btnAutoPlay_Tapped"/>
                <Button x:Name="btnLast" Content="&gt;&gt;" HorizontalAlignment="Stretch" VerticalAlignment="Stretch" FontFamily="Segoe UI Symbol" FontSize="24" Tapped="btnAutoPlay_Tapped"/>
                <MediaElement x:Name="mediaElementMove" Source="Sounds/move.wav" AudioCategory="BackgroundCapableMedia" Volume="10" AutoPlay="False"></MediaElement>
                <MediaElement x:Name="mediaElementDeadStone" Source="Sounds/deadstoneless.wav" AudioCategory="BackgroundCapableMedia" Volume="10" AutoPlay="False"></MediaElement>
            </StackPanel>

            <ProgressBar x:Name="progressBar"  IsIndeterminate="True" Grid.Column="1" Grid.Row="0" Visibility="Collapsed"></ProgressBar>
        </Grid>

        <VisualStateManager.VisualStateGroups>

            <!-- Visual states reflect the application's view state -->
            <VisualStateGroup x:Name="ApplicationViewStates">
                <VisualState x:Name="FullScreenLandscape"/>
                <VisualState x:Name="Filled"/>

                <!-- The entire page respects the narrower 100-pixel margin convention for portrait -->
                <VisualState x:Name="FullScreenPortrait">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource PortraitBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>

                <!-- The back button and title have different styles when snapped -->
                <VisualState x:Name="Snapped">
                    <Storyboard>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="backButton" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedBackButtonStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                        <ObjectAnimationUsingKeyFrames Storyboard.TargetName="pageTitle" Storyboard.TargetProperty="Style">
                            <DiscreteObjectKeyFrame KeyTime="0" Value="{StaticResource SnappedPageHeaderTextStyle}"/>
                        </ObjectAnimationUsingKeyFrames>
                    </Storyboard>
                </VisualState>
            </VisualStateGroup>
        </VisualStateManager.VisualStateGroups>
    </Grid>
</common:LayoutAwarePage>
